package com.inforich.orderManage.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inforich.carManage.domain.Driver;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.BookingEnum;
import com.inforich.orderManage.domain.Transport;
import com.inforich.orderManage.service.ITransportService;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.orderManage.mapper.BookingMapper;
import com.inforich.orderManage.domain.Booking;
import com.inforich.orderManage.service.IBookingService;

/**
 * 预约Service业务层处理
 *
 * @author hjm
 * @date 2022-10-26
 */
@Service
public class BookingServiceImpl extends ServiceImpl<BookingMapper, Booking> implements
    IBookingService {

  @Autowired
  private ITransportService transportService;

  @Override
  public Page<Booking> pageBooking(Page<Booking> page, Map<String, Object> map) {
    return baseMapper.pageBooking(page, map);
  }

  @Override
  public List<Booking> listBooking(Map<String, Object> map) {
    return baseMapper.listBooking(map);
  }

  @Override
  public List<Booking> listLastBooking(Map<String, Object> map) {
    return baseMapper.listLastBooking(map);
  }

  @Override
  public Booking getLastBookingByProjectId(String projectId) {
    return baseMapper.getLastBookingByProjectId(projectId);
  }

  @Override
  public Booking getBookingById(String id) {
    Booking booking = baseMapper.selectBookingById(id);
    booking.setDriverNames(getDriverName(booking.getDrivers()));
    List<Transport> transportList = transportService.listByBookingId(id);
    booking.setTransportList(transportList);
    return booking;
  }

  @Override
  public int insertBooking(Booking booking) {
    return baseMapper.insertBooking(booking);
  }

  @Override
  public AjaxResult complete(String id) {
    // 判断该笔预约单有没有正在进行中的运输单
    List<Transport> transportList = transportService.listByBookingId(id);
    if (CollUtil.isNotEmpty(transportList)) {
      long count = transportList.stream().filter(t -> t.getEndTime() == null).count();
      if (count > 0) {
        return AjaxResult.error("有尚未结束的运输单，不可进行结束操作");
      }
    }
    boolean b = this.update(new LambdaUpdateWrapper<Booking>()
        .set(Booking::getState, BookingEnum.COMPLETE.getDbData())
        .set(Booking::getCompleteTime, new Date())
        .eq(Booking::getId, id)
        .eq(Booking::getState, BookingEnum.CONDUCT.getDbData()));
    return b ? AjaxResult.success() : AjaxResult.error();
  }

  /**
   * 司机车牌号
   */
  public String getDriverName(List<Driver> drivers) {
    return CollUtil.isEmpty(drivers) ? ""
        : drivers.stream().map(Driver::getCarNumber).collect(Collectors.joining(","));
  }

}
